package com.etong.citymap.util.log;

import android.annotation.SuppressLint;
import android.content.Context;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * Created by Administrator on 2016/10/18.
 */
public class LogUtil {
    private static FileUtil fileUtil;
    private static DateFormat dateFormat;
    private static String V = "[VERBOSE]";
    private static String D = "[DEBUG]";
    private static String I = "[INFO]";
    private static String W = "[WARN]";
    private static String E = "[ERROR]";
    private static String ENTER = "\n";
    //private static String SPACE = "\t";
    private static final String SEPARATOR = ",";
    private static final int VERBOSE = 1;
    private static final int DEBUG = 2;
    private static final int INFO = 3;
    private static final int WARN = 4;
    private static final int ERROR = 5;
    private static final int LEVEL = WARN;

    @SuppressLint("SimpleDateFormat") 
    public static void registUtil(Context context) {
        fileUtil = new FileUtil(context);
        dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    }

    public static void unregistUtil() {
        fileUtil = null;
        dateFormat = null;
    }

    private static String getCurTimeStr() {
        return "[" + dateFormat.format(System.currentTimeMillis()) + "]";
    }

    private static synchronized void startWriteThread(final String systemOut) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    IOUtil.getInstance().write(fileUtil.getLogFile(), systemOut);
                } catch (FileNotFoundException e) {
                    LogUtil.e(e);
                }
            }
        }).start();
    }

    public static void v(String out) {
    	if (LEVEL <= VERBOSE) {
    		StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
	        synchronized (fileUtil) {
	            String systemOut = getCurTimeStr() + V + out + ENTER;
	            startWriteThread(systemOut);
	        }
    	}
    }

    public static void d(String out) {
    	if (LEVEL <= DEBUG) {
        	StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
	        synchronized (fileUtil) {
	            final String systemOut = getCurTimeStr() + D + out + ENTER;
	            startWriteThread(systemOut);
	        }
    	}
    }

    public static void i(String out) {
    	if (LEVEL <= INFO) {
    		StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
	        synchronized (fileUtil) {
	            final String systemOut = getCurTimeStr() + I + out + ENTER;
	            startWriteThread(systemOut);
	        }
    	}
    }
    

    public static void w(String out) {
    	if (LEVEL <= WARN) {
    		StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
	        synchronized (fileUtil) {
	            final String systemOut = getCurTimeStr() + W + out + ENTER;
	            startWriteThread(systemOut);
	        }
    	}
    }

    public static void e(String out) {
    	if (LEVEL <= ERROR) {
    		StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
	        synchronized (fileUtil) {
	            final String systemOut = getCurTimeStr() + E + out + ENTER;
	            startWriteThread(systemOut);
	        }
    	}
    }
    
    public static void e(Exception e) {
    	if (LEVEL <= ERROR) {
    		StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
	        synchronized (fileUtil) {
	            final String systemOut = getCurTimeStr() + E + getErrorInfoFromException(e) + ENTER;
	            startWriteThread(systemOut);
	        }
    	}
    }
    
    private static String getErrorInfoFromException(Exception e) {
    	try {
		   StringWriter sw = new StringWriter();
		   PrintWriter pw = new PrintWriter(sw);
		   e.printStackTrace(pw);
		   sw.close();
		   pw.close();
		   return "\r\n" + sw.toString() + "\r\n";
	  } catch (Exception e2) {
		  return "ErrorInfoFromException";
	  } 
    }
    
   public static String getLineInfo(StackTraceElement stackTraceElement) {
		StringBuilder logInfoStringBuilder = new StringBuilder();
		// 获取类名.即包名+类名
		String className = stackTraceElement.getClassName();
		// 获取方法名称
		String methodName = stackTraceElement.getMethodName();
		// 获取生日输出行数
		int lineNumber = stackTraceElement.getLineNumber();

		logInfoStringBuilder.append("[");
		logInfoStringBuilder.append(className).append(".");
		logInfoStringBuilder.append(methodName).append(":");
		logInfoStringBuilder.append(lineNumber);
		logInfoStringBuilder.append("] ");
		return logInfoStringBuilder.toString();
	}
}

